$(function () {
    // alert('111')

    var q = {
        tagname: '',
        page: 1,
        limit: 8,
    }
    initArtList();
    //获取列表数据--文字
    function initArtList() {
        $.ajax({
            url: 'http://124.223.14.236:8060/admin/ydd_tag/list',
            method: 'GET',
            data: q,
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                // console.log(res);
                if (res.errno !== 0) {
                    return layer.msg(res.errmsg)
                }
                // console.log(res);
                //通过模板将数据渲染到页面
                var htmlStr = template('artListTpl', res.data)
                $('#tbodyTxt').html(htmlStr)
                renderPage(res.data.count)

            }
        })
    }

    //分页
    function renderPage(total) {
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage.render({
                elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
                count: total, //数据总数，从服务端得到
                limit: q.limit, //每页数
                curr: q.page, //页数
                limits: [2, 3, 4, 5],
                layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
                jump: function (obj, first) {
                    // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    // console.log(obj.limit); //得到每页显示的条数
                    q.page = obj.curr
                    q.limit = obj.limit
                    if (!first) { //首次不执行
                        initArtList();
                    }
                }
            });
        });
    }

    //添加
    var layerIndex = null;
    $('#addBtn').on('click', function () {
        // console.log('1');
        layerIndex = layer.open({
            title: '添加友情链接',
            type: 1,
            area: ['400px', '200px'],
            content: $('#tplAddCate').html(),
            // success: function () {
            //     form.render();
            // },
        });
        // console.log(111);
    })
    //添加提交
    $('body').on('submit', '#addCateForm', function (e) {
        e.preventDefault();
        var q = {
            'tagname': $('[name=tagname]').val(),
        };

        $.ajax({
            url: 'http://124.223.14.236:8060/admin/ydd_tag/add',
            method: 'POST',
            data: q,
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                console.log(res);
                initArtList();

            }
        })
    })


    //删除
    $('tbody').on('click', '#btnDel', function () {
        var id = $(this).attr('data-id'); //获取文章的id
        // console.log(id);
        var length = $('.btnDel').length;

        layer.confirm('删吗?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            $.ajax({
                url: 'http://124.223.14.236:8060/admin/ydd_tag/del?id=' + id,
                headers: {
                    //"x-token": localStorage.getItem("x-token") || "",
                    'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
                },
                success: function (res) {
                    console.log(res);
                    if (res.errno !== 0) {
                        return layer.msg(res.errmsg)
                    }
                    layer.msg(res.errmsg)
                    if (length === 1) {
                        q.pagenum = q.pagenum === 1 ? q.pagenum === 1 : q.pagenum - 1
                    }
                    initArtList();

                }
            })
            layer.close(index);
        });
    })


    //筛选
    $('#selectForm').on('submit', function (e) {
        e.preventDefault();
        var tagname = $('[name=tagname]').val();
        q.tagname = tagname;
        initArtList();
    })


})